<template>
  <div class="login">
    <div class="login-box">
      <div class="box-left">
        <img width="80%" src="../assets/bg3.png" alt />
      </div>
      <div class="box-right">
        <div>
          <h3 class="login-title">欢迎登录校友内推管理平台</h3>
          <a-form
            :form="form"
            v-show="!loginshow"
            @submit="phonelogin"
            style="padding: 25px 50px 5px 50px"
          >
            <a-form-item>
              <a-input
                v-decorator="[
                  'username',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入账号',
                        trigger: 'blur',
                      },
                    ],
                  },
                ]"
                placeholder="请输入账号"
              >
                <a-icon
                  slot="prefix"
                  type="user"
                  style="color: rgba(0, 0, 0, 0.25)"
                />
              </a-input>
            </a-form-item>
            <a-form-item>
              <a-input-password
                v-decorator="[
                  'password',
                  {
                    rules: [
                      {
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur',
                      },
                    ],
                  },
                ]"
                placeholder="请输入密码"
              >
                <a-icon
                  slot="prefix"
                  type="lock"
                  style="color: rgba(0, 0, 0, 0.25)"
                />
              </a-input-password>
            </a-form-item>
            <div
              style="
                display: flex;
                justify-content: space-between;
                align-items: center;
              "
            >
              <!-- <a-checkbox v-model="checked">记住账号</a-checkbox> -->
              <a-button
                type="primary"
                @click.native="phonelogin"
                style="width: 100%"
                >登 录</a-button
              >
            </div>
          </a-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { userLogin } from "../http/api";
export default {
  name: "login",
  data() {
    return {
      baseUrl: baseUrl,
      checked: false,
      loginshow: false,
      form: this.$form.createForm(this),
    };
  },
  mounted() {
    this.getlocalStorage();
  },
  methods: {
    // 登录
    getlocalStorage() {
      if (localStorage.getItem("username") === null) {
        this.checked = true;
      } else {
        this.form.setFieldsValue({
          username: localStorage.getItem("username"),
        });
        if (this.form.getFieldValue(username) !== "") {
          this.checked = true;
        } else {
          this.checked = true;
        }
      }
    },
    phonelogin(e) {
      if (this.checked === true) {
        localStorage.setItem("username", this.form.getFieldValue("username"));
      } else {
        localStorage.clear();
      }
      e.preventDefault();
      this.form.validateFields((err, values) => {
        if (!err) {
          const data = values;

          userLogin(data)
            .then((response) => {
              if (response.data.code === 0) {
                sessionStorage.clear();
                sessionStorage.setItem(
                  "accessToken",
                  response.data.data.accessToken
                );
                sessionStorage.setItem(
                  "refreshToken",
                  response.data.data.refreshToken
                );
                sessionStorage.setItem("username", response.data.data.username);
                sessionStorage.setItem("createUserId", response.data.data.id);
                this.$router.push("/mainPage");
              }
            })
            .catch(() => {});
        }
      });
    },
  },
};
</script>

<style  scoped>
.login {
  background-image: url("../assets/images/1.png");
  background-repeat: no-repeat;
  background-size: 100% 100%;
  width: 100vw;
  height: 100vh;
  position: relative;
}
.login-box {
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #ffffff;
  width: 925px;
  height: 455px;
  box-shadow: 5px 5px 10px 10px rgba(0, 0, 0, 0.2);
  display: flex;
  justify-content: space-between;
  border-radius: 10px;
}
.box-left {
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.box-right {
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-title {
  text-align: center;
  margin: 0;
  color: #000;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}
.showtext {
  line-height: 40px;
  color: #000000;
  margin: 0 0 0 20px;
  float: left;
}
</style>
